package com.mdd.common.utils;

import java.math.BigDecimal;

/**
 * @name: FormulaUtil
 * @description: 公式计算
 * @author: slien.zuo
 */
public class FormulaUtil {

    public static final int SCALE_EIGHT = 8;
    public static final int SCALE_FIVE = 6;
    public static final int SCALE_FOUR = 6;
    public static final int SCALE_THREE = 3;
    public static final int SCALE_TWO = 2;

    /**
     * 「int」计算环比
     *
     * @param value
     * @param lastValue
     * @return
     */
    public static double calculateIntegerMOM(Integer value, Integer lastValue) {
        if (value == null || IntegerUtil.vd(lastValue)) {
            return 0.0;
        }
        double base = (double) (value - lastValue) * 100 / lastValue;
        return roundDoubleValue(base);
    }

    /**
     * 「double」计算环比
     *
     * @param value
     * @param lastValue
     * @return
     */
    public static double calculateDoubleMOM(Double value, Double lastValue) {
        if (value == null || lastValue == null || lastValue == 0.0) {
            return 0.0;
        }
        double base = (value - lastValue) * 100 / lastValue;
        return roundDoubleValue(base);
    }

    /**
     * 「double」计算环比差值
     *
     * @param value
     * @param lastValue
     * @return
     */
    public static double calculateDoubleDiffer(Double value, Double lastValue) {
        if (value == null || lastValue == null || lastValue == 0.0) {
            return 0.0;
        }
        return roundThirdValue(value - lastValue);
    }

    /**
     * 计算占比
     *
     * @param value
     * @param totalValue
     * @return
     */
    public static Double calculateDegree(Integer value, Integer totalValue) {
        if (IntegerUtil.vd(value)) {
            return 0.0;
        }
        if (IntegerUtil.vd(totalValue)) {
            return 0.0;
        }
        double base = (double) value * 100 / totalValue;
        return roundDoubleValue(base);
    }

    /**
     * 计算占比
     *
     * @param value
     * @param totalValue
     * @return
     */
    public static Double calculateRate(Integer value, Integer totalValue) {
        if (IntegerUtil.vd(value)) {
            return 0.0;
        }
        if (IntegerUtil.vd(totalValue)) {
            return 0.0;
        }
        double base = (double) value * 100 / totalValue;
        return roundDoubleValue(base);
    }

    /**
     * 计算占比
     *
     * @param value
     * @param totalValue
     * @return
     */
    public static Double calculateRate(Double value, Integer totalValue) {
        if (value == null || value <= 0) {
            return 0.0;
        }
        if (IntegerUtil.vd(totalValue)) {
            return 0.0;
        }
        double base = value * 100 / totalValue;
        return roundDoubleValue(base);
    }

    /**
     * 计算占比
     *
     * @param value
     * @param totalValue
     * @return
     */
    public static Double calculateDiffer(Double value, Integer totalValue) {
        if (value == null || value <= 0) {
            return 0.0;
        }
        if (IntegerUtil.vd(totalValue)) {
            return 0.0;
        }
        double base = value / totalValue;
        return roundDoubleValue(base);
    }

    /**
     * 四舍五入 取 两位小数点
     *
     * @param value
     * @return
     */
    private static double roundDoubleValue(double value) {
        BigDecimal baseDecimal = new BigDecimal(value);
        return baseDecimal.setScale(SCALE_TWO, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 四舍五入 取 两位小数点
     *
     * @param value
     * @return
     */
    public static BigDecimal roundBigDecimal2Value(BigDecimal value) {
        return value.setScale(SCALE_TWO, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 四舍五入 取 两位小数点
     *
     * @param value
     * @return
     */
    public static BigDecimal roundBigDecimal8Value(BigDecimal value) {
        return value.setScale(SCALE_EIGHT, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 四舍五入 取 两位小数点
     *
     * @param value
     * @return
     */
    public static BigDecimal roundBigDecimal5Value(BigDecimal value) {
        return value.setScale(SCALE_FIVE, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 四舍五入 取 三位小数点
     *
     * @param value
     * @return
     */
    private static double roundThirdValue(double value) {
        BigDecimal baseDecimal = new BigDecimal(value);
        return baseDecimal.setScale(SCALE_THREE, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 计算差值
     *
     * @param value
     * @param lastValue
     * @return
     */
    public static Integer calculateIntegerDiffer(Integer value, Integer lastValue) {
        if (IntegerUtil.vd(value)) {
            value = 0;
        }
        if (IntegerUtil.vd(lastValue)) {
            lastValue = 0;
        }
        return value - lastValue;
    }

    /**
     * @param unitPrice: 单价
     * @param quantity:  数量
     * @param scale:     小数位
     * @name: multiply 乘法四舍五入
     * @description:
     * @return: java.math.BigDecimal
     * @since: 2020/11/16 12:18
     * @author: slien.zuo
     */
    public static BigDecimal multiply(BigDecimal unitPrice, Long quantity, int scale) {
        if (unitPrice == null) {
            unitPrice = BigDecimal.ZERO;
        }
        if (quantity == null) {
            quantity = 0L;
        }
        BigDecimal quantityDecimal = BigDecimal.valueOf(quantity);
        BigDecimal result = unitPrice.multiply(quantityDecimal);
        return result.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal multiply(BigDecimal unitPrice, Integer quantity, int scale) {
        if (unitPrice == null) {
            unitPrice = BigDecimal.ZERO;
        }
        if (quantity == null) {
            quantity = 0;
        }
        BigDecimal quantityDecimal = BigDecimal.valueOf(quantity);
        BigDecimal result = unitPrice.multiply(quantityDecimal);
        return result.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal multiply(String str, Long quantity, int scale) {
        BigDecimal unitPrice = BigDecimal.ZERO;
        if (str != null) {
            unitPrice = new BigDecimal(str);
        }
        if (quantity == null) {
            quantity = 0L;
        }

        BigDecimal quantityDecimal = BigDecimal.valueOf(quantity);
        BigDecimal result = unitPrice.multiply(quantityDecimal);
        return result.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal divide(BigDecimal amount, Long quantity, int scale) {
        if (amount == null) {
            return BigDecimal.ZERO;
        }
        if (quantity == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal quantityDecimal = BigDecimal.valueOf(quantity);
        return amount.divide(quantityDecimal, scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * @param param1:
     * @param param2:
     * @param scale:  小数位
     * @name: multiply 乘法四舍五入
     * @description:
     * @return: java.math.BigDecimal
     * @since: 2020/11/16 12:18
     * @author: slien.zuo
     */
    public static BigDecimal multiply(BigDecimal param1, BigDecimal param2, int scale) {
        if (param1 == null) {
            param1 = BigDecimal.ZERO;
        }
        if (param2 == null) {
            param2 = BigDecimal.ZERO;
        }
        BigDecimal result = param1.multiply(param2);
        return result.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * @param unitPrice: 单价
     * @param exchange:  汇率
     * @param scale:     小数位
     * @name: divide 除法四舍五入
     * @description:
     * @return: java.math.BigDecimal
     * @since: 2020/11/16 12:18
     * @author: slien.zuo
     */
    public static BigDecimal divide(BigDecimal unitPrice, BigDecimal exchange, int scale) {
        if (unitPrice == null || BigDecimal.ZERO.equals(unitPrice)) {
            return BigDecimal.ZERO;
        }
        if (exchange == null || BigDecimal.ZERO.equals(exchange)) {
            return BigDecimal.ZERO;
        }
        return unitPrice.divide(exchange, scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal add(BigDecimal param1, BigDecimal param2) {
        if (param1 == null) {
            param1 = BigDecimal.ZERO;
        }
        if (param2 == null) {
            param2 = BigDecimal.ZERO;
        }
        return add(param1, param2, null);
    }

    public static BigDecimal add(BigDecimal param1, BigDecimal param2, BigDecimal param3) {
        if (param1 == null) {
            param1 = BigDecimal.ZERO;
        }
        if (param2 == null) {
            param2 = BigDecimal.ZERO;
        }
        if (param3 == null) {
            param3 = BigDecimal.ZERO;
        }
        return param1.add(param2).add(param3);
    }

    public static BigDecimal subtract(BigDecimal param1, BigDecimal param2) {
        if (param1 == null) {
            param1 = BigDecimal.ZERO;
        }
        if (param2 == null) {
            param2 = BigDecimal.ZERO;
        }
        return param1.subtract(param2);
    }

    /**
     * @return int
     * @Author slien
     * @Description
     * @Date 16:22 2022/10/12
     * @Param [totalAmount, unit]
     **/
    public static int multiply(BigDecimal totalAmount, int unit) {
        if (totalAmount == null) {
            totalAmount = BigDecimal.ZERO;
        }
        BigDecimal result = totalAmount.multiply(new BigDecimal(unit));
        return result.intValue();
    }

    /**
     * @return int
     * @Author slien
     * @Description
     * @Date 17:13 2022/10/12
     * @Param [totalAmount, unit]
     **/
    public static BigDecimal divide(BigDecimal totalAmount, Integer unit) {
        if (totalAmount == null) {
            totalAmount = BigDecimal.ZERO;
        }
        BigDecimal result = totalAmount.divide(new BigDecimal(unit));
        return result.setScale(2);
    }
}
